<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>10-事件对象练习</title>
    <style>
        #box1 {
            width: 100px;
            height: 100px;
            background-color: #bfa;
            position: absolute;
        }
    </style>

    <script>
        window.onload = function () {

            /*需求:div跟随鼠标移动*/


            var box1 = document.getElementById("box1");

            document.onmousemove = function (event) {

                //解决兼容
                event = event || window.event;

                var st = document.body.scrollTop || document.documentElement.scrollTop;
                var sl = document.body.scrollLeft || document.documentElement.scrollLeft ;

                //获取鼠标的坐标
                var left = event.pageX;
                var top = event.pageY;

                //设置div的偏移量
                box1.style.left = left + "px";
                box1.style.top = top + st + "px";
            }

        }
    </script>
</head>

<body>
    <div id="box1"></div>
</body>

</html>